<?php declare(strict_types=1);


namespace PinWuYun\domain\service\config;

use Exception;
use PinWuYun\domain\dao\ConfigDao;
use PinWuYun\domain\logic\common\Logic;
use PinWuYun\domain\models\ConfigModel;

/**
 * 配置服务层
 * Creator : zhiJie
 * Date: 2021/7/6
 */
class config
{
    /** @var ConfigDao 配置表dao */
    protected $configDao;

    public function __construct()
    {
        $this->configDao = new ConfigDao();
    }

    /**
     * 获取一条配置数据
     * @param string $key
     * @return mixed
     */
    public function get(string $key)
    {
        // 检测为空就报错
        Logic::isEmpty($key);
        return $this->configDao->getConfig($key);
    }

    /**
     * 添加一条配置数据
     * @param string $key
     * @param string $value
     * @return ConfigModel
     * @throws Exception
     */
    public function addConfig(string $key, string $value): ConfigModel
    {
       return $this->configDao->addConfig($key, $value);
    }

    /**
     * 修改配置项目
     * @param string $key
     * @param string $value
     * @return bool
     * @throws Exception
     */
    public function setConfig(string $key, string $value): bool
    {
        return $this->configDao->setConfig($key, $value);
    }

    /**
     * 获取多个配置信息
     * @param array $keys
     * @return array
     */
    public function getConfigGroup(array $keys): array
    {
        $configList = $this->configDao->getConfigGroup($keys);
        if(!$configList) {
            return [];
        }
        return array_combine(array_column($configList,'key'), array_column($configList,'value'));
    }

    /**
     * 修改多个配置
     * @param array $configGroup [['key'=>'', 'value'=>'']]
     */
    public function setConfigGroup(array $configGroup): bool
    {
        return $this->configDao->setConfigGroup($configGroup);
    }
}